//
//  AppDelegate.m
//  caipiao_ios
//
//  Created by mc on 2018/6/29.
//  Copyright © 2018年 ZhangZhanbin. All rights reserved.
//

#import "AppDelegate.h"
#import "ByTabBarVC.h"
#import "WXApi.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    [self scrollAppearanceFit];
    
    self.window.rootViewController = [[ByTabBarVC alloc] init];
    
    
    [self weixinLoginConfig:launchOptions];
    
    [self configApp];
    
    
    NSLog(@"版本 = %@,id = %@",APP_VERSION,APP_SCHEME);
   
    return YES;
}

- (NSMutableArray *)selectedArray{
    if (_selectedArray == nil) {
        _selectedArray = [NSMutableArray arrayWithCapacity:1];
    }
    return _selectedArray;
}


#pragma mark - 基于ios11的全局适配
- (void)scrollAppearanceFit {
    UITableView.appearance.tableFooterView = UIView.new;
    if (@available(ios 11.0,*)) {
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
        UITableView.appearance.estimatedRowHeight = 0;
        UITableView.appearance.estimatedSectionFooterHeight = 0;
        UITableView.appearance.estimatedSectionHeaderHeight = 0;
    }
}


- (void)configApp{
    
    if (BY_USER && BY_USER.uid.length > 0) {
        
        [ByApiOther paySupportSuccess:^(ByNetBaseResult *result){
            
            NSLog(@"支付方式获取 = %@", result.data);
            NSArray *array = result.data[@"data"];
            NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
            NSData * data =  [NSKeyedArchiver archivedDataWithRootObject:array];
            [userDefaults  setObject:data forKey:KEY_Pay_Style];//存储设备列表
            [userDefaults synchronize];
            
        } failure:^(ByNetBaseResult *result) {
            
            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
        
        
        [ByMoneyApi userMoneyFollowSuccess:^(ByNetBaseResult *result) {
           
            NSLog(@"查询 = %@", result.data);
            NSDictionary *dic = result.data[@"data"];
            NSString *keyong = dic[@"keyong"];
            NSString *tixian = dic[@"tixian"];
            NSString *invitation_code = dic[@"invitation_code"];
            ByModelUser *user = BY_USER;
            user.withdrawals_money = tixian;
            user.user_money = keyong;
            user.invitation_code = invitation_code;
            [ByCacheUser setUserInfo:user];
            
        } failure:^(ByNetBaseResult *result) {

            [ByCoreToolAlert showFailureWithText:result.msg];
        }];
        
        
        [ByApiOther configAppSuccess:^(ByNetBaseResult *result) {
            NSLog(@"APP配置信息查询 %@", result.data);
            NSArray *array = result.data[@"data"];
            NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
            if ([array isKindOfClass:[NSArray class]]) {
                for (int i = 0; i < array.count; i++) {
                    NSDictionary *dic = [array objectAtIndex:i];
                    if ([dic[@"name"] isEqualToString:@"service"]) {
                        [userDefaults  setObject:dic[@"value"] forKey:KEY_SERVICE_INFO];
                    }
                }
            }
            [userDefaults synchronize];
            
        } failure:^(ByNetBaseResult *result) {
            //[ByCoreToolAlert showFailureWithText:result.msg];
        }];
    }
}



#pragma mark -- 微信登陆配置 --
- (void)weixinLoginConfig:(NSDictionary *)launchOptions {
    [WXApi registerApp:WEIXIN_APPID];
}

/*
 程序要实现和微信终端交互的具体请求与回应，因此需要实现WXApiDelegate协议的两个方法
 */
- (void)onReq:(BaseReq *)req{
    DLog(@"微信onReq");
}
- (void)onResp:(BaseResp *)resp{
    DLog(@"微信onResp");
    
    if (resp == nil) {
        NSLog(@"不能为空!");
        return;
    }
    
    // 登陆
    if([resp isKindOfClass:[SendAuthResp class]]) {
        
        SendAuthResp *temp = (SendAuthResp*)resp;
        if (temp.errCode== 0) {
            
            NSString *code = temp.code;
            NSString *state = temp.state;
            
            NSDictionary *dictionary = @{@"code":code,@"state":state};
            [[NSNotificationCenter defaultCenter] postNotificationName:@"weixinLogin" object:self userInfo:dictionary];
        }
    }
}


// 支持所有iOS系统
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
    
    return[WXApi handleOpenURL:url delegate:self];
}


//NOTE: 9.0以后使用新API接口
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options
{
    return [WXApi handleOpenURL:url delegate:self] ;
}


- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url{
    
    return [WXApi handleOpenURL:url delegate:self];
    
}

- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
}


- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}


- (void)applicationWillEnterForeground:(UIApplication *)application {
    
    
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
}


- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}


- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}


@end
